package com.hong.alert.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import system.notify.Admin;
import system.wrap_filter.Policy;
import system.wrap_filter.Standard;

public class ReferenceDataRepossitory implements DAO {

	private JdbcTemplate jdbcTemplate;

	public List<Policy> getPolicies() {
		return this.jdbcTemplate.query("select * from filteringpolicy",
				new RowMapper<Policy>() {
					public Policy mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Policy policy = new Policy();
						policy.setMessage(rs.getString("message"));
						policy.setSource(rs.getString("source"));
						policy.setState(rs.getString("state"));
						policy.setSubtype(rs.getString("subtype"));
						policy.setType(rs.getString("type"));
						policy.setTemperature(rs.getInt("temperature"));
						return policy;
					}
				});
	}

	@Override
	public void setDataSource(BasicDataSource dataSource) {
		// TODO Auto-generated method stub
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public void pushLog(Standard standard) {
		// TODO Auto-generated method stub
		if (standard != null) {
			String sql = "INSERT INTO systemlog "
					+ "(date,source, type, info) VALUES (?, ?, ?,?)";
			jdbcTemplate.update(sql,
					new Object[] { standard.getDate(), standard.getSource(),
							standard.getAttribute(), standard.toString() });
		}
	}

	@Override
	public void pushLog(ArrayList<Standard> standardList) {
		// TODO Auto-generated method stub
		for (int i = 0; i < standardList.size(); i++) {
			Standard standard = standardList.get(i);
			pushLog(standard);
		}
	}

	@Override
	public List<Admin> admins() {
		// TODO Auto-generated method stub
		return this.jdbcTemplate.query("select * from admin",
				new RowMapper<Admin>() {
					public Admin mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Admin admin = new Admin();
						admin.setType(rs.getString("type"));
						admin.setAddress(rs.getString("address"));
						return admin;
					}
				});
	}
}
